__author__ = 'shawhen'

from ..platform import what as m_what_platform


class Application(object):
    def __init__(self, args=[]):
        global app

        super(Application, self).__init__()

        self._windows = {}

        self._wnd_proc = None

        if "win" in m_what_platform.running_os["type"]:
            from ..sysapi.win32 import WinUser

            def _wnd_proc(hWnd, msg_code, wParam, lParam):
                if msg_code == WinUser.WM_DESTROY:
                    WinUser.PostQuitMessage(0)

                return WinUser.DefWindowProc(hWnd, msg_code, wParam, lParam)

            self._wnd_proc = _wnd_proc
        else:
            raise NotImplementedError

    def _register_window(self, main_window, *args, **kwargs):
        self._windows[main_window] = kwargs

    def exec(self):
        if "win" in m_what_platform.running_os["type"]:
            from ..sysapi.win32 import WinUser

            msg = WinUser.MSG()
            while True:
                get_msg_result = WinUser.GetMessage(msg, None, 0, 0)
                if get_msg_result is True:
                    translate_msg_result = WinUser.TranslateMessage(msg)
                    WinUser.DispatchMessage(msg)
                else:
                    break
            return msg.wParam
        else:
            raise NotImplementedError